<?php
/*	
 * PHP Script to send Xtify 2.x messages with Curl
 * Please read the associated README file on how to use this script
 * 
 * @AndersRetteras, march 2012
 * 
 */
class xtifyApi
{
public $msg;
public $nc;
public $android;
public $iphone;
public $blackberry;
public $response;
// constructor
     public function __construct($msg_type) {
    	switch ($msg_type)
		{
		case "notif_config":
		$this->msg = new PushNotification();
		$this->nc =$this->msg->pushMessage->notificationConfiguration;
		$this->android =$this->msg->pushMessage->notificationConfiguration->notificationContent->android;
		$this->iphone =$this->msg->pushMessage->notificationConfiguration->notificationContent->iphone;
		$this->blackberry =$this->msg->pushMessage->notificationConfiguration->notificationContent->blackberry;
		break;
		case "now":
		$this->msg = new pushNow();
		$this->nc =$this->msg->pushMessage;
		$this->iphone =$this->msg->pushMessage->content;
	 }
	 }
	 
	 
	public function create(){
	$url="http://api.xtify.com/1.2/notifconfig/create";
	unset($this->nc->id);
	$this->response=json_decode($this->transaction($url));
	//$this->response=$this->transaction($url);
	}	
	
	public function update(){
	
	$this->response=json_decode($this->transaction($url));
	}
	public function delete(){
	$url="http://api.xtify.com/1.2/notifconfig/delete";
	$this->response=json_decode($this->transaction($url));
	}
	public function activate(){
	$url="http://api.xtify.com/1.2/notifconfig/activate";
	$this->response=json_decode($this->transaction($url));
	}
	public function deactivate(){
	$url="http://api.xtify.com/1.2/notifconfig/deactivate";
	$this->response=json_decode($this->transaction($url));
	}
	public function listNC(){
	$url="http://api.xtify.com/1.2/notifconfig/list";
	$this->response=json_decode($this->transaction($url));
	
	}
	public function detail(){
	$url="http://api.xtify.com/1.2/notifconfig/detail";
	//$this->response=json_decode($this->transaction($url));
	$this->response=$this->transaction($url);
	}
	
	public function transaction($url){
		$content = $this->msg->getAsJSON();
		$ch = curl_init();

		curl_setopt($ch, CURLOPT_URL, $url);

		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, "$content");
		curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json")); 
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

		$output = curl_exec($ch);
		curl_close($ch);
		Yii::log($output,"info","system.web.CController");
		return $output;
	}
}
?>